using UnityEngine;
using JufTools;
using Cinemachine;

public class PlayerMotor : MonoBehaviour
{
    [Header("额外组件")]
    private PlayerInput input;
    private Animator anim;

    [Header("玩家模型特殊点")]
    public Transform rightHoldPos;
    public Transform leftHoldPos;
    public Transform EyePos;
    public Transform FootPos;

    [Header("玩家视野")]
    public Camera eyeCamera;
    private Vector3 screenCenter;

    [Header("玩家拾取")]
    public float pickRadius = 1f;
    private Collider[] pickItems => Physics.OverlapSphere(FootPos.transform.position, pickRadius, PickLayer.value);
    public LayerMask PickLayer;

    [Header("环境检测")]
    public LayerMask GroundLayer;
    public bool isOnGround => Physics.OverlapSphere(FootPos.transform.position, 0.5f, GroundLayer.value).Length > 0;
    public bool isOnSlope 
    {
        get
        {
            return
            Physics.Raycast(FootPos.transform.position,Vector3.down, out slopeHitInfo, 0.8f, GroundLayer.value) && 
            slopeHitInfo.normal != Vector3.up;
        }
    }
    private RaycastHit slopeHitInfo;

    [Header("玩家受力")]
    public float GravityValue = -10f;
    public Vector2 playerGravity;
    private Vector3 moveDirection;
	private CharacterController control;
    private float sightX, sightY;

    [Header("玩家攻击")]
    public LayerMask enemyLayer;
    public float defendCD = 4f;
    public float defaultAtkValue = 10;
    public float defaultAtkDis = 1f;
    public float defaultAtkSpeed = 1f;

    #region 缓存数据
    private RaycastHit atkHitInfo;
    private float leftTimer;
    private float rightTimer;
    private float currentAtkDis;
    private float currentAtkValue;
    private float currentDefendCD;
    #endregion
    private void Awake()
    {
        if(eyeCamera == null)
            eyeCamera = GetComponentInChildren<Camera>();
        input = GetComponent<PlayerInput>();
        anim = GetComponent<Animator>();
        control = GetComponent<CharacterController>();
        screenCenter = new Vector3(Screen.width/2, Screen.height/2, 0);
    }
    private void Update()
    {
        leftTimer += Time.deltaTime;
        rightTimer += Time.deltaTime;
        PlayerSightUpdate();
        Attack();
    }
    private void LoadPlayerPara()
    {

        currentAtkDis = defaultAtkDis;
        currentAtkValue = defaultAtkValue;
        currentDefendCD = defendCD;
    }
    public void PlayerSightUpdate()
    {
        sightX = input.MouseX *  Time.deltaTime;
        sightY -= input.MouseY * Time.deltaTime;
        sightY = Mathf.Clamp(sightY , -70,  70);
        EyePos.localEulerAngles = new Vector3(sightY, 0, 0);
        transform.Rotate(0, sightX, 0);
    }
    public void SetMoveVelocity(Vector2 value)
    {
        moveDirection = transform.forward * value.y + transform.right * value.x;
        control.Move(moveDirection * Time.deltaTime);
    }
    public void Jump(float jumpHeight)
    {
        playerGravity.y += Mathf.Sqrt(jumpHeight * -3.0f * GravityValue);
        //调用时, 必须在Update或FixedUpdate中同步调用UseGravity
    }
    public void UseGravity()
    {
        playerGravity.y += GravityValue * Time.deltaTime;
        control.Move(playerGravity * Time.deltaTime);
    }
    
    private void Attack()
    {
        if(input.IsPressFire)
        {
            anim.Play("Attack");

        }
        if(input.IsPressAim)
        {
            anim.Play("Defend");
        }
    }

    #if UNITY_EDITOR
    private void OnDrawGizmos()
    {
        Gizmos.color = new Color(0,0,0,0.4f);
        Gizmos.DrawSphere(FootPos.transform.position, 0.5f);
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, pickRadius);
    }
    #endif
}
